package com.funo.logic.generate.code;

import com.funo.logic.constant.Modifier;
import com.funo.logic.generate.wrapper.AnnotationClassWrapper;
import com.funo.logic.generate.wrapper.ClassWrapper;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 行代码生成
 * @since 2024年7月23日10:32:20
 * @author cms
 */
public class FieldCodeSupport extends AbstractCodeSupport implements CodeSupport {

    /**
     * 名称
     */
    private String name;
    /**
     * 当前类
     */
    private ClassWrapper type;
    private List<Modifier> modifiers = new ArrayList<>();

    private List<String> commentList = new ArrayList<>();
    /**
     * 初始化值
     */
    private String initialization;
    /**
     * 引用的注解
     */
    private List<AnnotationClassWrapper> annotationList = new ArrayList<>();

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ClassWrapper getType() {
        return type;
    }

    public void setType(ClassWrapper type) {
        this.type = type;
    }

    public String getInitialization() {
        return initialization;
    }

    public void setInitialization(String initialization) {
        this.initialization = initialization;
    }

    public List<Modifier> getModifiers() {
        return modifiers;
    }

    public void setModifiers(List<Modifier> modifiers) {
        this.modifiers = modifiers;
    }

    @Override
    public String generateCode() {

        addIndent();

        for (AnnotationClassWrapper annotation : annotationList) {
            codeBuffer.append(annotation.generateCode());
        }
        addNewLine();
        addIndent();
        if (isPublic(modifiers)) {
            codeBuffer.append("public ");
        }

        if (isProtected(modifiers)) {
            codeBuffer.append("protected ");
        }

        if (isPrivate(modifiers)) {
            codeBuffer.append("private ");
        }

        if (isStatic(modifiers)) {
            codeBuffer.append("static ");
        }

        if (isFinal(modifiers)) {
            codeBuffer.append("final ");
        }

        codeBuffer.append(type.getName());

        addSpace();
        codeBuffer.append(name);

        if (StringUtils.isNotEmpty(initialization)) {
            codeBuffer.append(" = ");
            codeBuffer.append(initialization);
        }

        addSemicolon();
        return codeBuffer.toString();
    }

    public void addAnnotation(AnnotationClassWrapper annotation) {
        annotationList.add(annotation);
    }

    public void addModifier(Modifier modifier) {
        modifiers.add(modifier);
    }

    public void addComment(String comment) {
        commentList.add(comment);
    }

    public List<AnnotationClassWrapper> getAnnotationList() {
        return annotationList;
    }

    public void setAnnotationList(List<AnnotationClassWrapper> annotationList) {
        this.annotationList = annotationList;
    }
}
